/* global window */
import modelExtend from 'dva-model-extend'
import queryString from 'query-string'
import { message } from 'antd'
import { query, update, remove, detail, reset } from 'services/user'
import { queryRole } from 'services/common'
import { pageModel } from './common'

export default modelExtend(pageModel, {
  namespace: 'user',

  state: {
    currentItem: {},
    roles: [],
    modalVisible: false,
    resetModalVisible: false,
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      dispatch({ type: 'queryDict' })
      history.listen((location) => {
        if (location.pathname === '/user') {
          const payload = queryString.parse(location.search) || { page: 1, pageSize: 10 }
          dispatch({
            type: 'query',
            payload,
          })
        }
      })
    },
  },

  effects: {

    * query ({ payload = {} }, { call, put }) {
      const { success, data, errorMsg } = yield call(query, payload)
      if (success) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.dataList,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.total,
            },
          },
        })
      } else {
        throw errorMsg
      }
    },

    * queryDict ({ payload = {} }, { call, put, select }) {
      const { roles } = yield select(_ => _.user)
      if (roles.length === 0) {
        const resData = yield call(queryRole, {})
        yield put({ type: 'updateState', payload: { roles: resData.data } })
      }
    },

    * detail ({ payload = {} }, { call, put }) {
      const { success, data, errorMsg } = yield call(detail, payload)
      if (success) {
        yield put({ type: 'updateState', payload: { currentItem: data, modalVisible: true } })
      } else {
        throw errorMsg
      }
    },

    * delete ({ payload }, { call, put }) {
      const data = yield call(remove, payload)
      if (data.success) {
        yield put({ type: 'query' })
        message.success('删除成功')
      } else {
        throw data.errorMsg
      }
    },

    * update ({ payload }, { call, put }) {
      const data = yield call(update, payload)
      if (data.success) {
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
        message.success('更新成功')
      } else {
        throw data.errorMsg
      }
    },

    * reset ({ payload }, { call, put }) {
      const data = yield call(reset, payload)
      if (data.success) {
        yield put({ type: 'hideResetModal' })
        message.success('重置成功')
      } else {
        throw data.errorMsg
      }
    },

  },

  reducers: {

    showModal (state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal (state) {
      return { ...state, modalVisible: false }
    },

    showResetModal (state, { payload }) {
      return { ...state, ...payload, resetModalVisible: true }
    },

    hideResetModal (state) {
      return { ...state, resetModalVisible: false }
    },
  },
})
